<template>
  <el-form
    ref="ruleFormRef"
    :model="ruleForm"
    status-icon
    @submit.prevent
    :rules="rules"
    label-width="120px"
    class="demo-ruleForm"
  >
    <el-row :gutter="20">
      <el-col :span="8">
        <el-form-item label="应用" prop="menu_id">
          <el-select v-model="ruleForm.menu_id" placeholder="应用">
            <el-option
              v-for="item in optionsApp"
              :disabled="disabled"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="菜单名称" prop="title">
          <el-input v-model="ruleForm.title" type="input" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="菜单类型" prop="type">
          <el-select v-model="ruleForm.type" placeholder="菜单类型">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="打开方式" prop="target">
          <el-select v-model="ruleForm.target" placeholder="菜单类型">
            <el-option
              v-for="item in optionsTarget"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="链接地址" prop="url">
          <el-input v-model="ruleForm.url" type="input" />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="图标" prop="icon">
          <el-input v-model="ruleForm.icon" type="input" />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>
<script lang="ts" setup>
import { reactive, ref, defineExpose, onMounted } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { getMainMenu } from '@/api'

interface IOption {
  value: string
  label: string
}
const disabled = ref(false)
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive({
  id: '',
  menu_id: '',
  parent_id: '',
  type: '',
  target: '',
  url: '',
  icon: '',
  title: ''
})
const optionsApp = ref<IOption[]>([])
const optionsTarget = ref<IOption[]>([
  {
    label: '当前窗口',
    value: '_self'
  },
  {
    label: '新窗口',
    value: '_blank'
  },
  {
    label: '父窗口',
    value: '_parent'
  },
  {
    label: '顶层窗口',
    value: '_top'
  }
])
const options = ref<IOption[]>([
  {
    label: 'URL链接',
    value: 'url'
  },
  {
    label: '页面',
    value: 'page'
  },
  {
    label: '分组标题',
    value: 'section'
  },
  {
    label: '分隔线',
    value: 'divider'
  },
  {
    label: '下拉菜单',
    value: 'dropdown'
  }
])
const rules = reactive<FormRules<typeof ruleForm>>({
  title: [
    {
      required: true,
      message: '请输入菜单名称',
      trigger: 'change'
    }
  ],
  menu_id: {
    required: true,
    message: '请选择应用',
    trigger: 'change'
  }
})

const getAppList = () => {
  getMainMenu({ pageNum: 1, pageSize: 9999 }).then((res: any) => {
    if (res.code === 200) {
      optionsApp.value = res.data.map((item: any) => {
        return {
          label: item.name,
          value: item.id
        }
      })
    }
  })
}

const setFormData = (data: any) => {
  Object.keys(ruleForm).forEach((key) => {
    if (data[key]) {
      ruleForm[key] = data[key]
    }
  })
}

const addForm = (row) => {
  ruleForm.parent_id = row.menu_id ? row.id : null
  ruleForm.menu_id = row.menu_id ? row.menu_id : row.id
  disabled.value = true
}

const init = () => {
  getAppList()
}

onMounted(() => {
  init()
})

defineExpose({
  ruleForm,
  setFormData,
  ruleFormRef,
  addForm
})
</script>
<style lang="less" scoped></style>
